package com.keon.jianbao.adapter;

import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.keon.jianbao.AppContext;
import com.keon.jianbao.R;
import com.keon.jianbao.base.SimpleAdapter;
import com.keon.jianbao.bean.Expert;
import com.keon.jianbao.util.StringUtils;
import com.keon.jianbao.util.TDevice;
import com.keon.jianbao.util.UILUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by keon on 2015/9/1.
 */
public class HomeExpertAdapter extends SimpleAdapter {

    public HomeExpertAdapter(ArrayList data) {
        initData(data);
    }

    static class ViewHolder {
        @Bind(R.id.home_expert_img)
        ImageView sdvImg;
        @Bind(R.id.home_expert_name)
        TextView tvName;
        @Bind(R.id.home_expert_favour)
        TextView tvFavour;
        @Bind(R.id.home_expert_info)
        TextView tvInfo;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    @Override
    protected View getRealView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null || convertView.getTag() == null) {
            convertView = getLayoutInflater(parent.getContext()).inflate(
                    R.layout.list_cell_home_expert, null);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }

        Expert expert = (Expert) mDatas.get(position);

//        Uri uri = Uri.parse(expert.getExpert_face());
//        vh.sdvImg.setImageURI(uri);
        ImageLoader.getInstance().displayImage(expert.getExpert_face(), vh.sdvImg, UILUtils.getOptions2());
        vh.tvName.setText(expert.getExpert_name());
        vh.tvFavour.setText("好评：" + expert.getGood());
        if (StringUtils.isEmpty(expert.getExpert_title())) {
            vh.tvInfo.setText(expert.getAppraisal_catalog());
        } else {
            vh.tvInfo.setText(expert.getExpert_title());
        }

        LinearLayout.LayoutParams laParams = (LinearLayout.LayoutParams) vh.sdvImg.getLayoutParams();
        DisplayMetrics dm = TDevice.getDisplayMetrics();
        laParams.height = (int) ((dm.widthPixels -
                TDevice.dpToPixel(AppContext.getResource().getDimension(R.dimen.global_padding)) * 2) / 2);
        vh.sdvImg.setLayoutParams(laParams);


        return convertView;
    }
}
